"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DESC/DESCRIBE
Case Name   : desc指定schema分析表结构,语法错误时合理报错
Create At   : 2022-10-08
Owner       : @zou_jialiang050
Description :
    1.创建兼容B库
    2.B库下创建schema
    3.B库下建表
    4.desc使用错误语法分析表结构
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
History     :
    modified by @zou_jialiang050,2023.01.18，研发代码变更，更新报错信息
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DESC(unittest.TestCase):
    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.database = 'd_compatibility_desc_0023'
        self.schema = 's_compatibility_desc_0023'
        self.table = 't_compatibility_desc_0023'

    def test_desc(self):
        text1 = '-----step1:创建兼容B库 expect:成功-----'
        self.log.info(text1)
        create_database = self.pri_sh.execut_db_sql(
            f'''drop database if exists {self.database};
            create database {self.database} dbcompatibility='B';''')
        self.log.info(create_database)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_database,
                      '执行失败' + text1)

        text2 = '-----step2:B库下创建schema expect:成功-----'
        self.log.info(text2)
        create_schema = self.pri_sh.execut_db_sql(
            f'''drop schema if exists {self.schema};
            create schema {self.schema};''', dbname=self.database)
        self.log.info(create_schema)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, create_schema,
                      '执行失败' + text2)

        text3 = '-----step3:B库下建表 expect:成功-----'
        self.log.info(text3)
        create_table = self.pri_sh.execut_db_sql(
            f'''drop table if exists {self.schema}.{self.table};
            create table {self.schema}.{self.table}(c_int int default null,
            c_test varchar(10) default ' ');''', dbname=self.database)
        self.log.info(create_table)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, create_table,
                      '执行失败' + text3)

        text4 = '-----step4:desc使用错误语法分析表结构 expect:合理报错-----'
        self.log.info(text4)
        desc_table = self.pri_sh.execut_db_sql(
            f'''desc {self.schema} {self.table};''', dbname=self.database)
        self.log.info(desc_table)
        self.assertIn(f'ERROR:  syntax error '
                      f'at or near "{self.schema}"',
                      desc_table, '执行失败' + text4)

    def tearDown(self):
        text5 = '-----step5:清理环境 expect:成功-----'
        self.log.info(text5)
        clean_environment = self.pri_sh.execut_db_sql(
            f'''drop schema {self.schema} cascade;''', dbname=self.database)
        self.log.info(clean_environment)
        clean_environment1 = self.pri_sh.execut_db_sql(
            f'''drop database {self.database};''')
        self.log.info(clean_environment1)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, clean_environment,
                      '执行失败' + text5)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_environment1,
                      '执行失败' + text5)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

